
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Marwen
 */
public class HistoriqueTrader extends javax.swing.JFrame {

    DefaultTableModel model1;
    Administration adm;
    public HistoriqueTrader(Administration ad) {
        initComponents();
        adm=ad;
        model1 = (DefaultTableModel) tab.getModel();
        DbManip.remplirComboBoxADeuxChamps(choixTrader, "trader", "nom_trader", "prenom_trader", "Choisir un trader");

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        choixTrader = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tab = new javax.swing.JTable();
        panheader = new javax.swing.JPanel();
        home = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel3.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N
        jLabel3.setText("Choisir Trader ");

        choixTrader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                choixTraderActionPerformed(evt);
            }
        });

        tab.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Grade", "Date"
            }
        ));
        jScrollPane1.setViewportView(tab);

        panheader.setBackground(new java.awt.Color(0, 153, 153));

        home.setBackground(new java.awt.Color(0, 153, 153));
        home.setFont(new java.awt.Font("Vani", 1, 12)); // NOI18N
        home.setForeground(new java.awt.Color(255, 255, 255));
        home.setText("Acceuil");
        home.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                homeActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(0, 0, 0));
        jLabel1.setFont(new java.awt.Font("Vani", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("   Administrateur: ");

        jLabel2.setFont(new java.awt.Font("Vani", 1, 14)); // NOI18N
        jLabel2.setText("NET TRADING");

        javax.swing.GroupLayout panheaderLayout = new javax.swing.GroupLayout(panheader);
        panheader.setLayout(panheaderLayout);
        panheaderLayout.setHorizontalGroup(
            panheaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panheaderLayout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 145, Short.MAX_VALUE)
                .addComponent(home, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        panheaderLayout.setVerticalGroup(
            panheaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(home, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(155, 155, 155)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(73, 73, 73)
                .addComponent(choixTrader, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(90, 90, 90))
            .addComponent(panheader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panheader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(choixTrader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 146, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void choixTraderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_choixTraderActionPerformed
        try {
            // ***** Recupérer matricule Trader ********************     
                    String ch = (String) choixTrader.getSelectedItem();
                    if(ch.equalsIgnoreCase("-- Choisir un trader--")){
                        DbManip.viderListe(tab);
                        return;
                    }
                    DbManip.viderListe(tab);
                    String ch1 = ch.substring(0, ch.indexOf(" "));
                    String ch2 = ch.substring(ch.indexOf(" ") + 1);

                    DbManip.requete = "SELECT MATRICULE FROM TRADER WHERE NOM_TRADER='" + ch2 + "' and PRENOM_TRADER ='" + ch1 + "'";
                    DbManip.executer(DbManip.requete);
                    int mat = 0;
                    while (DbManip.rs.next()) {
                        mat = DbManip.rs.getInt(1);
                    }
                    
                    DbManip.requete="select ID_GRADE, DATE_OBTENTION_GRADE from trader_grade where matricule='"+mat+"' order by DATE_OBTENTION_GRADE  ";
                    DbManip.executer(DbManip.requete);
                    while(DbManip.rs.next()){
                          String date= DbManip.rs.getString(2);
                          String grade = DbManip.rs.getString(1);
                          switch (grade) {

                                case "1":
                                    grade = "Junior";
                                    break;
                                case "2":
                                    grade ="Senior" ;
                                    break;
                                case "3":
                                    grade = "Confirmé";
                                    break;
                            }
                          
                    model1.addRow(new String[]{ grade,date});            
            }
        }
        catch (SQLException ex) {
            Logger.getLogger(HistoriqueTrader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_choixTraderActionPerformed

    private void homeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_homeActionPerformed
        adm.setLocation(this.getLocation());
        adm.setVisible(true);
        this.setVisible(false);

        // TODO add your handling code here:
    }//GEN-LAST:event_homeActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox choixTrader;
    private javax.swing.JButton home;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panheader;
    private javax.swing.JTable tab;
    // End of variables declaration//GEN-END:variables
}
